<?php
require_once('../inc/connect.php');
$section = 'User';
echo '<h1>Register</h1>';

$submit = $_POST['submit'];

$email = strtolower(strip_tags($_POST['email']));
$password = strip_tags($_POST['password']);
$repassword = strip_tags($_POST['repassword']);
$fname = strip_tags($_POST['first_name']);
$lname = strip_tags($_POST['last_name']);
$created = date("Y-m-d H:i:s");

if($submit)
{

    $connect = mysql_connect(DB_HOST, DB_USER, DB_PASS);
    mysql_select_db(DB_NAME);
    
    $emailcheck = mysql_query("SELECT email FROM users WHERE email='$email'");
    $count = mysql_num_rows($emailcheck);
    
    if($count != 0)
    {
        die("Email already registered!");
    }
    
    if($email && $password && $repassword && $fname && $lname)
    {
        if($password == $repassword)
        {
            if(strlen($fname) > 64 || strlen($lname) > 64)
            {
                echo '<p>Length of first or last name is too long!</p>';
            }
            else
            {
                if(strlen($password) < 6 || strlen($password) > 24)
                {
                    echo '<p>Password must be between 6 and 24 characters!</p>';
                }
                else
                {
                    $password = md5($password);
                    $repassword = md5($repassword);
                    
                    $queryreg = mysql_query("INSERT INTO users (created, email, password, first_name, last_name) VALUES(NOW(), '$email', '$password', '$fname','$lname')");
                    
                    die('You have been registered! <a href="index.php">Return to index.</a>');
                }
            }
        }
        else
            echo '<p>Passwords do not match!</p>';
    }
    else
        echo '<p>Please fill in all fields!</p>';
}

?>
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <title>Register</title>
    <link rel="stylesheet" type="text/css" href="../css/reset.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
  </head>

  <body>
<?php include('../inc/nav.php'); ?>
<?php include('../inc/header.php'); ?>
    <form action="register.php" method="POST">
      <table>
        <tr>
          <td>Email</td>
          <td>
            <input type="text" name="email" maxlength="127
            value="<?=$email?>" />
          </td>
        </tr>
        <tr>
          <td>Password</td>
          <td>
            <input type="password" name="password" maxlength="64" />
          </td>
        </tr>
        <tr>
          <td>Repeat password</td>
          <td>
            <input type="password" name="repassword" maxlength="64" />
          </td>
        </tr>
        <tr>
          <td>First Name</td>
          <td>
            <input type="text" name="first_name" maxlength="64"
            value="<?=$fname?>" />
          </td>
        </tr>
        <tr>
          <td>Middle Initial</td>
          <td><input type="text" name="middle_initial" maxlength="1" /></td>
        </tr>
        <tr>
          <td>Last Name</td>
          <td>
            <input type="text" name="last_name" maxlength="64"
            value="<?=$lname?>" />
          </td>
        </tr>
      </table>
      <input type="submit" name="submit" value="Register" />
    </form>
<?php include('../inc/footer.php'); ?>
  </body>
</html>
